import Vue from 'vue'

import Cookies from 'js-cookie'

import 'normalize.css/normalize.css' // A modern alternative to CSS resets

import Element from 'element-ui'
// import 'element-ui/lib/theme-chalk/index.css'
// import '@/assets/custom-theme/index.css'
import '../theme/index.min.css'

import '@/styles/index.scss' // global css

import App from './App'
import router from './router'
import store from './store'

import i18n from './lang' // Internationalization
import './icons' // icon
import './errorLog' // error log
import './permission' // permission control
// import './mock' // simulation data
import axios from 'axios'
import NP from 'number-precision'

// 滚动条
import HappyScroll from 'vue-happy-scroll'
import 'vue-happy-scroll/docs/happy-scroll.css'

import * as filters from './filters' // global filters
// import UEditor from './views/mass/UEditor.vue'
axios.defaults.withCredentials = true
axios.defaults.headers['x-requested-with'] = 'new'

Vue.use(HappyScroll)
Vue.use(Element, {
  size: Cookies.get('size') || 'medium' // set element-ui default size
  // i18n: (key, value) => i18n.t(key, value)
})
Vue.prototype.axios = axios
// register global utility filters.
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})

Vue.config.productionTip = false
// Vue.component('UEditor', UEditor)

// Axios 拦截器
axios.interceptors.response.use(
  response => {
    if(response.data.code == 401) {
      router.push({ path: '/login'})
    }
    return response
  }, err => {
    return Promise.reject(err);
  });

  Number.prototype.toFixed = function(index){
    index = index ? index : 0;
    var x = Math.pow(10,index)
    return  Math.floor(this * x ) / x ;
  }
  //乘法
  Number.prototype.times = function(index){
    return  NP.times(this, index);
  }

  //除法
  Number.prototype.divide = function(index){
    return  NP.divide(this, index);
  }

Date.prototype.format = function(format){
    var o = {
      "M+" : this.getMonth()+1, //month
      "d+" : this.getDate(), //day
      "h+" : this.getHours(), //hour
      "m+" : this.getMinutes(), //minute
      "s+" : this.getSeconds(), //second
      "q+" : Math.floor((this.getMonth()+3)/3), //quarter
      "S" : this.getMilliseconds() //millisecond
    }

    if(/(y+)/i.test(format)) {
      format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    }

    for(var k in o) {
      if(new RegExp("("+ k +")").test(format)) {
        format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
      }
    }
    return format;
}

new Vue({
  el: '#app',
  router,
  store,
  i18n,
  render: h => h(App)
})
